package com.zhufei.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhufei.dao.AboutUsDao;

public class AboutUsServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AboutUsServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		if(action == null){
			
		}
		if(action.equals("add")){
			this.add(request,response);
		}
		if(action.equals("edit")){
			this.edit(request,response);
		}

	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException { 
		response.setContentType("text/html;charset=utf8");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		AboutUsDao aboutUs = new AboutUsDao();
		boolean isSucc = aboutUs.updateAboutUs("1", title, content);
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '编辑关于我们信息成功！ ');window.location.href= 'admin/adminIndex.jsp'; </script> "); 
			out.flush(); 
			out.close(); 			
		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '编辑关于我们信息失败！ ');window.location.href= 'admin/adminIndex.jsp'; </script> "); 
			out.flush(); 
			out.close(); 			
		}
		//response.sendRedirect("admin/adminIndex.jsp");
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf8");
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		AboutUsDao aboutUs = new AboutUsDao();
		boolean isSucc = aboutUs.updateAboutUs("1", title, content);
		if(isSucc){
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '添加关于我们信息成功！ ');window.location.href= 'admin/adminIndex.jsp'; </script> "); 
			out.flush(); 
			out.close(); 
		}else{
			PrintWriter   out   =   response.getWriter(); 
			out.write( " <script   language=javascript> alert( '添加关于我们信息失败！ ');window.location.href= 'admin/adminIndex.jsp'; </script> "); 
			out.flush(); 
			out.close(); 			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
